package tgc.edu.yzy.web.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import tgc.edu.yzy.custom.Custom;
import tgc.edu.yzy.entity.BmsRegister;
import tgc.edu.yzy.entity.Book;
import tgc.edu.yzy.entity.User;
import tgc.edu.yzy.security.User2;
import tgc.edu.yzy.service.BookService;
import tgc.edu.yzy.service.RegisterService;
import tgc.edu.yzy.service.UserService;
/***
 * 登录的控制器
 * 主要实现的功能
 * 1.登录跳转
 * 2.安全退出跳转
 * 3.跳转到无需登录的页面  比如 注册 404，500提示页面等等等。。。
 * @author 936642284
 *
 */
@Controller
public class LoginController {
	@Autowired
	private Custom custom;
	@Autowired
	private BookService bookservice;
	@Autowired
	private RegisterService registerservice;
	/***
	 * 跳转到首页
	 * @return
	 */
	@RequestMapping(value= "/")
	private String index(ModelMap map) {
		List<Book> books=bookservice.findAll();
		map.put("books", books);
		return "index";
	}
	@RequestMapping(value= "/index")
	private String index2(ModelMap map) {
		List<Book> books=bookservice.findAll();
		map.put("books", books);
		return "index";
	}
	/***
	 * 跳转到登录页面
	 * @return
	 */
	@RequestMapping(value="/login")
	private String Login() {
		return "login";
	}
	/*** 
	 * 1.成功登录
	 * 2.为了防止用户刷新 重新提交一次表单  重新跳转到另一个页面
	 * @return
	 */
	@RequestMapping(value="/interim")
	private String home() {
		String uuid=null;
		//custom.denglu(uuid);
		return "redirect:home";
	}
	/***
	 * 1.登录
	 * 2.这里时转到前端页面去的 首页
	 * @return
	 */
	@RequestMapping(value="/home")
	private String interim() {
		return "view/home";
 	}
	@RequestMapping(value="out")
	private String out() {
		String uuid="登出";
		//custom.denglu(uuid);
		return "redirect:logout";
	}
	@RequestMapping(value="CheckCode")
	@ResponseBody
	private boolean user() {
		SecurityContext ctx = SecurityContextHolder.getContext();
	    Authentication auth = ctx.getAuthentication();
	    User2 userer =null;
	    try {
	    	userer= (User2) auth.getPrincipal();
	    	if(userer!=null) {
	    		return true;
	    	}else {
	    		return false;
	    	}
		} catch (Exception e) {
			return false;
		}
	}
	/***
	 * load 把修改密码的表单load过去
	 * @return
	 */
	@RequestMapping(value="pwdedit")
	private String pwdedit() {
		return "view/pwdedit";
	}
	/***
	 * 查询用户密码是否正确
	 * @param password
	 * @return
	 */
	@RequestMapping(value="QueryPassword")
	@ResponseBody
	private boolean QueryPassword(String password) {
		BmsRegister users=registerservice.findById(custom.userid());
		BCryptPasswordEncoder encoder=new BCryptPasswordEncoder();
		boolean f = encoder.matches(password,users.getPassword());
		return f;
	}
	/***
	 * 修改密码
	 * 修改密码以后直接退书
	 * @return
	 */
	@RequestMapping(value="editsave")
	private String editsave(String xmm) {
		BCryptPasswordEncoder encoder=new BCryptPasswordEncoder();
		BmsRegister users=registerservice.findById(custom.userid());
		users.setPassword(encoder.encode(xmm));
		registerservice.save(users);
		return "redirect:logout";
	}
	@RequestMapping(value="/head")
	private String head() {
		return "head";
	}
}
